/*
 * @Author: 刘苗
 * @Description:
 * @Date: 2021-09-08 13:49:58
 */
var app = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http, {
  transports: ["websocket"],
});

app.get("/", function (req, res) {
  res.send("<h1>Welcome Realtime Server</h1>");
});

// 保存用户
const users = {};
// 在线人数
const onlineCount = 0;
// 保存客户端对应的socket
const sockS = {};
//保存每一个房间的绘图
const canvaS = {};

io.on("connection", (socket) => {
  console.log("用户连接...");
  socket.on("join", (data) => {
    console.log("接收用户加入数据--->", data,socket.id);
    if (!users[data.roomid]) {
      users[data.roomid] = [];
    }
    let obj = {
      account: data.account,
      id: socket.id,
    };
    let arr = users[data.roomid].filter((v) => v.account === data.account);
    if (!arr.length) {
      users[data.roomid].push(obj);
    }
    sockS[data.account] = socket;
    console.log(users,users[data.roomid])
    io.in(data.roomid).emit("joined", users[data.roomid], data.account, socket.id); // 发给房间内所有人
  });
});

http.listen(3001, function () {
  console.log("listening on *:3001");
});
